@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@CMAKE_PROJECT_NAME@-targets.cmake")

find_program(WAYLAND_SCANNERPP wayland-scanner++)
if(NOT WAYLAND_SCANNERPP)
    message(FATAL_ERROR "wayland-scanner++ not found. Please install the waylandpp package.")
endif()

function(add_wayland_protocol out_var)
    set(oneValueArgs PROTOCOL)
    set(multiValueArgs INCLUDE)
    cmake_parse_arguments(ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if(NOT ARGS_PROTOCOL)
        message(FATAL_ERROR "add_wayland_protocol requires PROTOCOL argument")
    endif()

    get_filename_component(_infile ${ARGS_PROTOCOL} ABSOLUTE)
    get_filename_component(_base_name ${_infile} NAME_WE)

    set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_base_name}.h")
    set(_code   "${CMAKE_CURRENT_BINARY_DIR}/${_base_name}.cpp")

    set(_scanner_args "")
    foreach(extra IN LISTS ARGS_INCLUDE)
        list(APPEND _scanner_args -x ${extra})
    endforeach()

    list(APPEND _scanner_args ${_infile} ${_header} ${_code})

    add_custom_command(
        OUTPUT ${_header} ${_code}
        COMMAND ${WAYLAND_SCANNERPP} ${_scanner_args}
        DEPENDS ${_infile}
        VERBATIM
    )

    set_source_files_properties(${_header} ${_code} GENERATED)
    set_property(SOURCE ${_header} ${_code} PROPERTY SKIP_AUTOMOC ON)

    if (TARGET ${out_var})
        target_sources(${out_var} PRIVATE ${_header} ${_code})
    else()
        list(APPEND ${out_var} ${_header} ${_code})
        set(${out_var} ${${out_var}} PARENT_SCOPE)
    endif()
    include_directories(${CMAKE_CURRENT_BINARY_DIR})
endfunction()

